<?php

namespace app\admin\controller\integral;

use app\admin\model\finance\extract\School;
use app\admin\model\finance\extract\taskOut;
use app\common\controller\Backend;
use app\common\model\Category;
use think\Session;
use app\admin\model\finance\extract\Week;

/**
 * 周任务个人管理
 *
 * @icon fa fa-circle-o
 */
class Outtasks extends Backend
{
    
    /**
     * Task模型对象
     * @var \app\admin\model\finance\extract\Task
     */
    protected $model = null;
    protected $model_task = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->loadlang('finance.extract.week');
        $this->model = new taskOut();
         $this->model_task = new \app\admin\model\finance\extract\Task;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("diskList", $this->model->diskList());

    }


    /**
     * 添加
     */
    public function release($ids){
        $user = session::get('admin');

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try {
                   
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    //if ($this->model->where(array('task_id'=>$ids))->find())$this->error("此任务已存在");
                    $post = $this->request->post();
                    $params['province']=$post['province'];
                    $params['city']=$post['city'];
                    $params['area']=$post['area'];
                    $params['task_id']=$ids;
                 
                    $params['publish']=$user['userid'];
                    $params['z_publish']=$user['userid'];

                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success("操作成功",'index');
                    } else {
                        $this->error($this->model->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }else {

            $task = $this->model_task;
            $taskInfo = $task->where(array('id' => $ids, 'userid' => $user['userid'], 'type' => 1))->find();
            if (!$taskInfo) return $this->error('您当前没有任务');
            $weekDb = new Week();
            $weekInfo = $weekDb->find($taskInfo['extract_week']);
            $dep = TableField('AuthDepartment', 'name', $weekInfo['department']);
            //标题，和公司名称
            $gongsi['name'] = $user['username'];
            $gongsi['dep'] = $dep;
            $this->view->assign("weekid", $taskInfo['extract_week']);
            $this->assign('ids', $ids);
            $this->view->assign("gongsi", $gongsi);

            $models = new School();
            $week['regLevelList']=$models->getRegLevelList();
            $week['LevelList']=$models->getLevelList();
            $week['rankingLevelList']=$models->getRankingLevelList();

            $this->view->assign("week", $week);


            return $this->view->fetch();
        }

    }
 /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $user = session::get('admin');
        $row = $this->model->get($ids);
      
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $task = $this->model_task;
        $taskInfo = $task->where(array('id' => $ids, 'userid' => $user['userid'], 'type' => 1))->find(9);
        if (!$taskInfo) return $this->error('您当前没有任务');
        $weekDb = new Week();
        $weekInfo = $weekDb->find($taskInfo['extract_week']);
        $dep = TableField('AuthDepartment', 'name', $weekInfo['department']);
        //标题，和公司名称
        $gongsi['name'] = $user['username'];
        $gongsi['dep'] = $dep;
        $this->view->assign("weekid", $taskInfo['extract_week']);
        $this->assign('ids', $ids);
        $this->view->assign("gongsi", $gongsi);

        $models = new School();
        $week['regLevelList']=$models->getRegLevelList();
        $week['LevelList']=$models->getLevelList();
        $week['rankingLevelList']=$models->getRankingLevelList();
          //print_r($row->grade);die;
       /* echo '<pre>';
        var_dump($row->toArray());*/
        $this->view->assign("week", $week);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    /**
     * 查看
     */
    //和跑人
    public function runer($id){

        if ($this->request->isAjax()) {
            $userDb = new \app\admin\model\finance\extract\User();

            $where['extract_week']=$id;
            $uinfo = session::get('admin');

            $where['userid']=array('neq',$uinfo['userid']);
            list($whe, $sort, $order, $offset, $limit) = $this->buildparams();
            $info = $userDb->where($where)->order($sort, $order)->limit($offset, $limit)->select();
            $info = collection($info)->toArray();

            $result = array("total" => $userDb->where($where)->count(), "rows" => $info);

            return json($result);


        }
    }


     /**
     * 查看
     */
    public function index($ids=null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as &$row) {
                $row->visible(['id','school_id','status','createtime','updatetime']);
            }
            //print_r($this->model->getLastSql());die;
            $list = collection($list)->toArray();
            //print_r($list);die;
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
       
        $this->view->assign('ids',$ids);
        return $this->view->fetch();
    }
     /**
     * 提交审核
     */
    public function delete($ids = NULL){ 
    
        if ($ids) {
            $pk = $this->model->getPk();
    
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();
            $count = 0;
            foreach ($list as $k => $v) {
              if($v->status == 1 || $v->status == 2){
                $count += $v->delete();
              }
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were updated'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
}
